<?php

class HoCTableRow {
	
	private $rank;
	private $team;
	private $term;
	private $type;
	private $update;
	private $uptodate;
	private $color;
	
	public function __construct($rank, $team, $term, $type) {
		$this->setRank($rank);
		$this->setTeam($team);
		$this->term = $term;
		$this->type = $type;
		$this->setUpdate($team->getUpdate());
	}
	
	public function getRank() {
		return $this->rank;
	}
	
	public function setRank($rank) {
		$this->rank = $rank;
	}
	
	public function getTeam() {
		return $this->team;
	}
	
	public function setTeam($team) {
		$this->team = $team;
	}
	
	public function getCountry() {
		return $this->team->getLeague()->getDivision()->getCountry();
	}
	
	public function getLeague() {
		return $this->team->getLeague();
	}
	
	public function getFlags() {
		switch ($this->type) {
			case CONST_AWAY:	return $this->getTeam()->getAwayFlags();
			case CONST_HOME:	return $this->getTeam()->getHomeFlags();
			case CONST_AND:		return $this->getTeam()->getAndFlags();
			case CONST_OR:		return $this->getTeam()->getOrFlags();
			case CONST_PLUS:	return $this->getTeam()->getPlusFlags();
			default:			return $this->getTeam()->getAwayFlags();
		}		
	}
	
	public function getCoolness() {
		switch ($this->type) {
			case CONST_AWAY:	return $this->getTeam()->getAwayCoolness();
			case CONST_HOME:	return $this->getTeam()->getHomeCoolness();
			case CONST_AND:		return $this->getTeam()->getAndCoolness();
			case CONST_OR:		return $this->getTeam()->getOrCoolness();
			case CONST_PLUS:	return $this->getTeam()->getPlusCoolness();
			default:			return $this->getTeam()->getAwayCoolness();
		}
	}
	
	public function getUpdate() {
		return $this->update;
	}
	
	public function setUpdate($update) {
		try {
			$diff = $this->timeDiff($update, time());
			if ($diff['years'] > 0) {
				if ($diff['years'] != 38) {
					$this->update = $diff['years'] . " year"; 
					if ($diff['weeks'] != 1) $this->update .= "s";
				}
				else throw new NeverUpdatedException();
				$this->setUpToDate(false);
				$this->setColor('#ff0000');
			}
			else if ($diff['months'] > 0) {
				$this->update = $diff['months'] . " month"; 
				if ($diff['months'] != 1) $this->update .= "s";
				$this->setUpToDate(false);
				$this->setColor('#ff0000');
			}
			else if ($diff['weeks'] > 0) {
				$this->update = $diff['weeks'] . " week"; 
				if ($diff['weeks'] != 1) $this->update .= "s";
				$this->setUpToDate(false);
				$this->setColor('#ffc600');
			}
			else if ($diff['days'] > 0) {
				$this->update = $diff['days'] . " day"; 
				if ($diff['days'] != 1) $this->update .= "s";
				$this->setUpToDate(false);
				$this->setColor('#fffc00');
			}
			else if ($diff['hours'] > 0) {
				$this->update = $diff['hours'] . " hour"; 
				if ($diff['hours'] != 1) $this->update .= "s";
				$this->setUpToDate(true);
				$this->setColor('#72ff00');
			}
			else if ($diff['minutes'] > 0) {
				$this->update = $diff['minutes'] . " minute"; 
				if ($diff['minutes'] != 1) $this->update .= "s";
				$this->setUpToDate(true);
				$this->setColor('#30ff00');
			}
			else if ($diff['seconds'] >= 0) {
				$this->update = $diff['seconds'] . " second"; 
				if ($diff['seconds'] != 1) $this->update .= "s";
				$this->setUpToDate(true);
				$this->setColor('#30ff00');
			}
			$this->update .= " ago";
		}
		catch (NeverUpdatedException $nue) {
			$this->update = "Never";
			$this->setUpToDate(false);
			$this->setColor('#cccccc');
		}
	}
	
	public function isUpToDate() {
		return $this->uptodate;
	}
	
	private function setUpToDate($bool) {
		$this->uptodate = $bool;
	}
	
	public function getColor() {
		return $this->color;
	}
	
	private function setColor($color) {
		$this->color = $color;
	}
	
	private function timeDiff($time1, $time2) {
		$diff = array(
			'years' => 0,
			'months' => 0,
			'weeks' => 0,
			'days' => 0,
			'hours' => 0,
			'minutes' => 0,
			'seconds' =>0
		);
		
		foreach (array('years','months','weeks','days','hours','minutes','seconds') as $unit) {
			while(TRUE) {
				$next = strtotime("+1 $unit", $time1);
				if ($next < $time2) {
					$time1 = $next;
					$diff[$unit]++;
				}
				else break;
			}
		}
		return $diff;
	}

}

?>